<?php

/**
 * This is the model class for table "proveedor".
 *
 * The followings are the available columns in table 'proveedor':
 * @property integer $idProveedor
 * @property string $nombre_compania
 * @property string $nombre_contacto
 * @property string $direccion
 * @property integer $localidad
 * @property string $telefono
 * @property string $fax
 * @property string $email
 * @property string $posicion_iva
 * @property string $cuit
 * @property integer $forma_pago
 * @property string $descuento
 * @property integer $plazo
 * @property string $notas
 * @property integer $suspendido
 *
 * The followings are the available model relations:
 * @property Compra[] $compras
 * @property Localidad $localidad0
 * @property PosicionIva $posicionIva
 * @property Formadepago $formaPago
 */
class Proveedor extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'proveedor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre_compania, direccion, localidad', 'required'),
			array('localidad, forma_pago, plazo, suspendido', 'numerical', 'integerOnly'=>true),
			array('nombre_compania, nombre_contacto, direccion', 'length', 'max'=>100),
			array('telefono', 'length', 'max'=>20),
			array('fax, email', 'length', 'max'=>45),
			array('posicion_iva', 'length', 'max'=>1),
			array('cuit', 'length', 'max'=>11),
			array('descuento', 'length', 'max'=>10),
			array('notas', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('idProveedor, nombre_compania, nombre_contacto, direccion, localidad, telefono, fax, email, posicion_iva, cuit, forma_pago, descuento, plazo, notas, suspendido', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'compras' => array(self::HAS_MANY, 'Compra', 'proveedor'),
			'localidad0' => array(self::BELONGS_TO, 'Localidad', 'localidad'),
			'posicionIva' => array(self::BELONGS_TO, 'PosicionIva', 'posicion_iva'),
			'formaPago' => array(self::BELONGS_TO, 'Formadepago', 'forma_pago'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idProveedor' => 'Id Proveedor',
			'nombre_compania' => 'Nombre Compania',
			'nombre_contacto' => 'Nombre Contacto',
			'direccion' => 'Direccion',
			'localidad' => 'Localidad',
			'telefono' => 'Telefono',
			'fax' => 'Fax',
			'email' => 'Email',
			'posicion_iva' => 'Posicion Iva',
			'cuit' => 'Cuit',
			'forma_pago' => 'Forma Pago',
			'descuento' => 'Descuento',
			'plazo' => 'Plazo',
			'notas' => 'Notas',
			'suspendido' => 'Suspendido',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('localidad0','formaPago');
		$criteria->compare('idProveedor',$this->idProveedor);
		$criteria->compare('nombre_compania',$this->nombre_compania,true);
		$criteria->compare('nombre_contacto',$this->nombre_contacto,true);
		$criteria->compare('direccion',$this->direccion,true);
		$criteria->compare('localidad0.nombre',$this->localidad,true);
		$criteria->compare('telefono',$this->telefono,true);
		$criteria->compare('fax',$this->fax,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('posicion_iva',$this->posicion_iva,true);
		$criteria->compare('cuit',$this->cuit,true);
		$criteria->compare('formaPago.descripcion',$this->forma_pago,true);
		$criteria->compare('descuento',$this->descuento,true);
		$criteria->compare('plazo',$this->plazo);
		$criteria->compare('notas',$this->notas,true);
		$criteria->compare('suspendido',$this->suspendido);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,'sort'=>array(
					'defaultOrder'=>'nombre_compania ASC',)
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Proveedor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
